import { defineStore } from 'pinia';
import { ref } from 'vue';
import { USER_INFO } from '@/utils/constant';

type UserInfo = {
  username: string;
  avatar: string;
  role: string;
};
export const useUserStore = defineStore('user', () => {
  const userInfo = ref<UserInfo | null>(null);

  let user = localStorage.getItem(USER_INFO);
  if (user) {
    userInfo.value = JSON.parse(user);
  }

  function setUserInfo(info) {
    userInfo.value = info;
    localStorage.setItem(USER_INFO, JSON.stringify(info));
  }
  function clearUserInfo() {
    userInfo.value = null;
    localStorage.removeItem(USER_INFO);
  }

  return {
    userInfo,
    setUserInfo,
    clearUserInfo
  };
});
